﻿//这里的代码，是此控件操作复选择框的，主要作用是当父结点被选择时，子结点根着对应变化
var ignoreEvents = false;
var selectTBS = '';
var tv,tree; //节点树
var isInit=true;//标记是否是页面初始加载
function nodeEventHandler(treeview, node, eventType) {
    if (ignoreEvents) return;
    ignoreEvents = true;
    if (!isInit) {
        if (eventType == "checked") {
            setChildrenCheckBox(node, true);
            setTimeout(setStatic, 500); //延迟0.5秒再统计
        } else if (eventType == "unchecked") {
            setChildrenCheckBox(node, false);
            setTimeout(setStatic, 500); //延迟0.5秒再统计
        }
    }
    ignoreEvents = false;
}
function setChildrenCheckBox(parentNode, value) {
    var childNodes = parentNode.getChildNodes();
    for (var i = 0; i < childNodes.length; i++) {
        var node = childNodes[i];
        node.setChecked(value);
        if (value) { //选中就直接加在字符串后面
            selectTBS += node.getValue() + ',';
        }
        else {
            selectTBS.replace(node.getValue() + ',', ''); //取消选择就替换字符串中该基站TBS
        }
        setChildrenCheckBox(node, value);
    }
    if (childNodes.length == 0) {
        parentNode.setChecked(value);
        selectTBS = selectTBS.replace(new RegExp(parentNode.getValue() + ',', 'g'), ''); //取消选择就替换字符串中该基站TBS
        if (value) { //选中就直接加在字符串后面
            selectTBS += parentNode.getValue() + ',';
        }
    }
    setSelectedTBSToCookie(selectTBS);
}
function setParentCheckBox(node, value) {
    var parentNode = node.getParent();
    if (parentNode == null) return;
    parentNode.setChecked(value);
    if (!value || allSiblingChecked(parentNode))
        setParentCheckBox(parentNode, value);
}
function allSiblingChecked(node) {
    var nodes = node.getSiblingNodes();
    for (var i = 0; i < nodes.length; i++) {
        if (!nodes[i].getChecked())
            return false;
    }
    return true;
}
///*将选中的基站TBS字符串保存到COOKIE中*/
function setSelectedTBSToCookie() {
    window.parent.SetCookie('zhwg_TBSXN', selectTBS);
}

function setStatic() {
    window.parent.document.getElementById('ImpMonitorIndexRight').contentWindow.initPage();
}
/*设置节点选中*/
function setNodeTree(treeview) {
    if (!tv) {
        tv = treeview;
        tree = treeview;
    }
}
/*获取节点名称*/
function getNodeName(neid) {
    var nodes = tv.getNodes();
    for (var i = 0, length = nodes.length; i < length; i++) {
        var childNodes = nodes[i].getChildNodes();
        for (var j = 0, count = childNodes.length; j < count; j++) {
            if (childNodes[j].getValue() == neid) {
                return childNodes[j].getText();
            }
        }
    }
}
/*节点选中*/
function setNodeChecked(treeview) {

}
/*设置节点选中*/
function setNodeCheckedInit() {
    var selectedTBS = window.parent.GetCookie('zhwg_TBSXN');
    if (selectedTBS && tv) {
        selectedTBS = selectedTBS.replace(/;/g, '');
       // selectTBS = selectedTBS;
        var nodes = tv.getNodes();
        for (var i = 0, length = nodes.length; i < length; i++) {
            var childNodes = nodes[i].getChildNodes();
            for (var j = 0, count = childNodes.length; j < count; j++) {
                if (selectedTBS.indexOf(childNodes[j].getValue() + ',') > -1) {
                    setChildrenCheckBox(childNodes[j], true);
                    // selectedTBS = selectedTBS.replace(new RegExp(childNodes[j].getValue() + ',', 'g'), '');
                }
                else {
                    setChildrenCheckBox(childNodes[j], false );
                }
            }
        }
    }
    isInit = false;
   // setTimeout(setStatic, 200);
}
function txtBlur() {
    window.parent.document.getElementById('ImpMonitorIndexRight').contentWindow.setParam($('#txtMonitorTime').val(), $('#txtMonitorHistoryTime').val(), $('#txtChangeTime').val(), $('#txtFreshTime').val());
}

$(function() {
    $('input[type=text]').bind('blur', function() {
        txtBlur();
        setMonitorHistoryToCookie($('#txtMonitorHistoryTime').val());
    });
    var d = new Date();
    $('#txtMonitorTime').val(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()).bind('click', function() {
        WdatePicker({ dateFmt: 'yyyy-M-d' });
    });
    var d = getMonitorHistoryFromCookies();
    $('#txtMonitorHistoryTime').val(d).bind('click', function() {
        WdatePicker({ dateFmt: 'yyyy-M-d', maxDate: '#F{$dp.$D(\'txtMonitorTime\',{d:-1});}' });
    });
});

/*将选中的历史对比时间字符串保存到COOKIE中*/
function setMonitorHistoryToCookie(time) {
    window.parent.SetCookie('zhwg_TBSXN_MonitorHistory', time.replace(/-0/g, '-'));
}
/*获取历史对比时间*/
function getMonitorHistoryFromCookies() {
    var mht = window.parent.GetCookie('zhwg_TBSXN_MonitorHistory');
    if (!mht) {
        var d = new Date();
        d = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 1);
        mht = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
    }
    return mht;
}